package com.yxq.mdt.app.modules.personal.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.orhanobut.hawk.Hawk;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.constant.SPKey;
import com.yxq.mdt.app.glide.GlideHelper;
import com.yxq.mdt.app.modules.personal.bean.MyPersonalBean;
import com.yxq.mdt.app.modules.personal.bean.RealNameResultBean;
import com.yxq.mdt.app.modules.personal.model.RealNameModel;
import com.yxq.mdt.app.modules.personal.presenter.IRealNameContract;
import com.yxq.mdt.app.modules.personal.presenter.RealNamePresenter;
import com.yxq.mdt.app.modules.personal.ui.anchor.PersonalOpenChatAc;
import com.yxq.mdt.app.modules.personal.ui.anchor.PersonalRealNameAc;
import com.yxq.mdt.app.modules.personal.ui.anchor.PersonalRealNameSucceedAc;
import com.yxq.mdt.app.modules.personal.ui.anchor.PersonalWithdrawAc;
import com.yxq.mdt.app.modules.personal.ui.anchor.PersonalWithdrawSucceedAc;
import com.yxq.mdt.app.modules.webview.ui.MiWebViewActivity;
import com.yxq.mdt.app.widgets.CircleImageView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 主播中心
 */
public class MainPersonalAnchorAc extends BaseActivity<RealNamePresenter> implements IRealNameContract.View {

    @BindView(R.id.titlebar)
    CommonTitleBar titlebar;
    @BindView(R.id.personal_anchor_head)
    CircleImageView personalAnchorHead;
    @BindView(R.id.personal_anchor_name)
    TextView personalAnchorName;
    @BindView(R.id.personal_anchor_sex)
    ImageView personalAnchorSex;
    @BindView(R.id.personal_anchor_become)
    LinearLayout personalAnchorBecome;
    @BindView(R.id.personal_anchor_real_name)
    LinearLayout personalAnchorRealName;
    @BindView(R.id.personal_anchor_open)
    LinearLayout personalAnchorOpen;
    @BindView(R.id.personal_anchor_collector)
    LinearLayout personalAnchorCollector;
    int requestType = 0;//0默认 1 实名认证 2开通密聊

    @Override
    public int getLayoutId() {
        return R.layout.activity_main_personal_anchor;
    }

    @Override
    public void initView() {
        MyPersonalBean myPersonalBean = Hawk.get(SPKey.SP_KEY_USER_MY_INFO);
        GlideHelper.setLoadImage(mActivity, myPersonalBean.getHeadimg(), 0, personalAnchorHead);
        switch (myPersonalBean.getSex()) {
            case 1:
                personalAnchorSex.setImageResource(R.mipmap.xb_nan);
                break;
            case 2:
                personalAnchorSex.setImageResource(R.mipmap.xb_nv);
                break;
            default:
        }
        if (myPersonalBean.getGsName().isEmpty()) {
            personalAnchorName.setText("测试用户");
        } else {
            personalAnchorName.setText(myPersonalBean.getGsName());
        }

    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mPresenter = new RealNamePresenter(this, new RealNameModel());
    }

    @OnClick({R.id.personal_anchor_become, R.id.personal_anchor_real_name, R.id.personal_anchor_open, R.id.personal_anchor_collector})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.personal_anchor_become:
                //成为主播
                String url = "https://yxq.quai69.com/static/compass.html";
                Intent it = new Intent(mActivity, MiWebViewActivity.class);
                it.putExtra("url", url);
                startActivity(it);
                break;
            case R.id.personal_anchor_real_name:
                //实名认证
                requestType = 1;
                showLoadingDialog(false);
                mPresenter.getCardNoAndName();
                break;
            case R.id.personal_anchor_open:
                //开通密聊
                requestType = 2;
                showLoadingDialog(false);
                mPresenter.getCardNoAndName();
                break;
            case R.id.personal_anchor_collector:
                //提现信息采集
                requestType = 3;
                showLoadingDialog(false);
                mPresenter.getCardNoAndName();
                break;
        }
    }

    //一般不带参数的跳转activity
    public void intentAc(Class<?> cls) {
        Intent intent = new Intent(mActivity, cls);
        startActivity(intent);
    }

    @Override
    public void onLoadSuccess(String data) {

    }

    @Override
    public void onLoadFailed(String msg) {

    }

    @Override
    public void onLoadRealNameSuccess(RealNameResultBean data) {
        hideLoadingDialog();
        if (data != null) {
            if (requestType == 3) {
                if (!TextUtils.isEmpty(data.getIdentityCardName()) && !TextUtils.isEmpty(data.getIdentityCardNo()) && !TextUtils.isEmpty(data.getIdentityCardFront()) && !TextUtils.isEmpty(data.getIdentityCardBack())) {
                    //跳转提现信息认证成功页面
                    Intent intent = new Intent(mActivity, PersonalWithdrawSucceedAc.class);
                    intent.putExtra("name", data.getIdentityCardName());
                    intent.putExtra("number", data.getIdentityCardNo());
                    intent.putExtra("frontImg", data.getIdentityCardFront());
                    intent.putExtra("backImg", data.getIdentityCardBack());
                    startActivity(intent);
                } else {
                    intentAc(PersonalWithdrawAc.class);
                }
                return;
            }
            if (!TextUtils.isEmpty(data.getIdentityCardName()) && !TextUtils.isEmpty(data.getIdentityCardNo())) {

                switch (requestType) {
                    case 1:
                        //跳转认证成功页面
                        Intent intent = new Intent(mActivity, PersonalRealNameSucceedAc.class);
                        intent.putExtra("name", data.getIdentityCardName());
                        intent.putExtra("number", data.getIdentityCardNo());
                        startActivity(intent);
                        break;
                    case 2:
                        intentAc(PersonalOpenChatAc.class);
                        break;
                }
            } else {
                //实名认证
                intentAc(PersonalRealNameAc.class);
            }
        } else {
            //实名认证
            intentAc(PersonalRealNameAc.class);
        }
    }

    @Override
    public void onLoadRealNameFailed(String msg) {
        hideLoadingDialog();
        ToastUtils.showShort(msg + "");
    }
}
